/* eslint-disable no-prototype-builtins */
/**
 * axios拦截器
 * <liuyang@3clear.com>
 * 说明：处理处理异常数据以及token更新
 * 2020-10-28
 * */
import axios from "axios";
import store from "@/store/";
const CancelToken = axios.CancelToken;

// http请求拦截器
axios.interceptors.request.use(
  (config) => {
    // 项目请求需要携带token时给请求增加Authorization
    if (config) {
      config.headers["Authorization"] = store.state.index.token;
    }
    // 设置请求取消方法便于功能中取消请求（功能中使用方式this.$store.cancelToken()）
    config.cancelToken = new CancelToken(function executor(c) {
      store.state.index.cancelTokens.push(c); // 更新待取消的请求集合
    });

    return config;
  },
  (err) => {
    return Promise.error(err);
  }
);
// http响应拦截器
axios.interceptors.response.use(
  (response) => {
    /** *********token更新 start***********/
    if (response.headers && response.headers.hasOwnProperty("Authorization")) {
      let token = response.headers.Authorization;
      store.dispatch("set_token", token);
    }
    if (response.headers && response.headers.hasOwnProperty("authorization")) {
      let token = response.headers.authorization;
      store.dispatch("set_token", token);
    }
    /** *********token更新 end ***********/

    return response;
  },
  (err) => {
    if (err && err.message && err.message.includes("timeout")) {
      err.HttpRequestMessage = "请求超时，请稍后再试";
    }

    if (err && err.response) {
      switch (err.response.status) {
        case 400:
          err.HttpRequestMessage = "请求错误";
          break;
        case 401:
          err.HttpRequestMessage = "未授权，请登录";
          break;
        case 403:
          err.HttpRequestMessage = "拒绝访问";
          break;
        case 404:
          err.HttpRequestMessage = `请求地址出错: ${err.response.config.url}`;
          break;
        case 408:
          err.HttpRequestMessage = "请求超时";
          break;
        case 500:
          err.HttpRequestMessage = "服务器内部错误";
          break;
        case 501:
          err.HttpRequestMessage = "服务未实现";
          break;
        case 502:
          err.HttpRequestMessage = "网关错误";
          break;
        case 503:
          err.HttpRequestMessage = "服务不可用";
          break;
        case 504:
          err.HttpRequestMessage = "网关超时";
          break;
        case 505:
          err.HttpRequestMessage = "HTTP版本不受支持";
          break;
        default:
          err.HttpRequestMessage = "网络异常，请稍后再试！";
      }
      /** *********token更新 start***********/
      // let response = err.response;
      // if (response.headers && response.headers.hasOwnProperty('Authorization')) {
      //   let token = response.headers.Authorization,
      //     time = new Date().getTime() + sysConfig.failureDuration;
      //   store.dispatch('set_token', token);
      //   store.dispatch('set_valid_time', time);
      // }
      // if (response.headers && response.headers.hasOwnProperty('authorization')) {
      //   let token = response.headers.authorization,
      //     time = new Date().getTime() + sysConfig.failureDuration;
      //   store.dispatch('set_token', token);
      //   store.dispatch('set_valid_time', time);
      // }
      /** *********token更新 end ***********/
    }
    return Promise.reject(err);
  }
);
